package com.peaksport.framework.map;

import java.util.HashMap;
import java.util.Map;

import com.peaksport.framework.exception.PKDevException;

/**
 * 不区分大小写有序Map
 * 实现原理：扩展ListMap,将key值转换成小写字母
 * 
 * @author jinbin_guo  2019年4月17日 上午11:32:08
 *
 * @param <V>
 */
@SuppressWarnings("serial")
public class ListNoCaseMap<V> extends ListMap<V> {
	
	private Map<String,String> keyMap = new HashMap<>();

    public V get(String key) {
    	if (key == null) 
   		 	throw PKDevException.throwException("ListNoCaseMap.get(key),key不能为空");
    	if (!(key instanceof String)) throw PKDevException.throwException("ListNoCaseMap.get(key),key[%s]类型错误,必须为String类型",key.getClass().getName());
        String newKey = keyMap.get(key.toLowerCase());
        return super.get(newKey);
    }

    public void add(String key,V value) {
    	if (key == null) 
   		 	throw PKDevException.throwException("ListNoCaseMap.get(key),key不能为空");
        String newKey = key.toLowerCase();
        keyMap.put(newKey,key);
        super.add(key,value);
    }

    public void addALL(ListNoCaseMap<V> otherListNoCaseMap)  {
    	if (otherListNoCaseMap == null || otherListNoCaseMap.isEmpty()) return;
        for (int i = 0,len = otherListNoCaseMap.size();i < len;i++) {
            String key = otherListNoCaseMap.getKey(i);
            V value= otherListNoCaseMap.get(key);
            add(key,value);
        }
    }

    public V remove(int index) {
    	if (index >= keyMap.size()) 
    		throw PKDevException.throwException("ListNoCaseMap.remove(index),index[%d]超出ListNoCaseMap集合大小[%d]",index,keyMap.size()-1);
        String key = getKey(index);
        return remove(key);
    }

    public V remove(String key) {
    	if (key == null)  throw PKDevException.throwException("ListNoCaseMap.get(key),key不能为空");
        String newKey = keyMap.remove(key.toLowerCase());
        return super.remove(newKey);
    }

    public void clear() {
        keyMap.clear();
        super.clear();
    }

}
